#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{
    pid_t id = fork();
    if(id < 0)
    {
        perror("fork");
        return 1;
    }
    else if(id == 0)
    {
        int cnt = 5;
        while(cnt--)
        {
            printf("I am a child, pid: %d, ppid: %d\n", getpid(), getppid());
            sleep(1);
        }
        exit(11); // stdlib.h
    }
    else
    {
        while(true)
        {
            int status = 0;
            pid_t ret = waitpid(id, &status, WNOHANG);
            if(ret < 0)
            {
                printf("wait failed!\n");
                break;
            }
            else if(ret > 0)
            {
                if(WIFEXITED(status))
                {
                    printf("The process exits normally, pid: %d, coredump:%d\n", WIFEXITED(status), (status>>7)&1);
                }
                else
                {
                    printf("The process exception occurred!, coredump:%d\n", (status>>7)&1);
                }
                break;
            }
            else
            {
                printf("waiting...\n");
                sleep(1);
            }
        }
    }

    return 0;
}